layui.use(['element', 'table', 'laytpl', 'form'], function () {
    var $ = layui.$, form = layui.form;

    $(".second").pageslide({direction: "left", modal: true});
    var items = new Array();
    var healthDataArray = new Array();
    /*监听提交事件是否被点击*/
    form.on('submit(sub)', function (data) {
        if (items.length==0){
            layer.msg("请勾选服务项目");
            return false;
        }
        $.ajax({
            url: "/records/save",
            data: {"records": "[" + items.join(",") + "]", "healthDatas": healthDataArray.join("@")},
            type: 'post',
            dataType: 'json',
            success: function (data) {
                if (data.code == "200") {
                    layer.msg("添加成功");
                    parent.layer.close(parent.oo)
                }
                if (data.code == "0") {
                    layer.msg("失败")
                }
            }
        });
        return false;
    });


    /**
     * 监控复选框事件
     */
    form.on('checkbox(checked)', function (data) {
        $(".healthDatabox").empty();
        var value = data.value;
        var arr = value.split('@');
        var type = arr[0];
        var id = arr[1];

        //如果取消复选框
        if (!data.elem.checked) {
            if (type=="2"){
                if (healthDataArray != null && healthDataArray.length > 0) {
                    for (var i = 0; i < healthDataArray.length; i++) {
                        if (id == JSON.parse(healthDataArray[i]).itemId) {
                            healthDataArray.splice(i, 1);
                            i = -1;
                        }
                    }
                }
            }
            if (items != null && items.length > 0) {
                for (var i = 0; i < items.length; i++) {
                    if (id == JSON.parse(items[i]).itemId) {
                        items.splice(i, 1);
                        i = -1;
                    }
                }

            }
        }
        //如果选中复选框
        if (data.elem.checked) {
            items.push('{"itemId":' + id + ',"type":' + type + '}');

            if (type == "2") {
                $(".healthDatabox").empty();
                $.ajax({
                    url: "/records/healthDataItems",
                    data: {serviceItemId: id},
                    type: 'get',
                    dataType: 'json',
                    async: false,
                    success: function (rs) {
                        if (rs.code == 200) {
                            $.each(rs.data, function (i, v) {
                                v.standardValues = v.standardValues ? v.standardValues : '无';
                                $(".healthDatabox").append(
                                    '                             <div class="healthDataItem">' +
                                    '                                <div >' +
                                    '                                    <label class="must">' + v.name + '</label>' +
                                    '                                    <div>' +
                                    '                                        <input type="text" name="value"   maxlength="4" placeholder="请输入结果" autocomplete="off" class="layui-input">' +
                                    '                                        <input type="hidden" name="standardValues" value="' + v.standardValues + '" />' +
                                    '                                        <input type="hidden" name="healthDataItemId"  value="' + v.id + '" />' +
                                    '                                        <input type="hidden" name="healthDataitemName" value="' + v.name + '" />' +
                                    '                                        <input type="hidden" name="itemId" value="' + id + '" />' +
                                    '                                    </div>' +
                                    '                                    <div style="margin-top:20px;">' +
                                    '                                        <select name="nature"  class="layui-input">' +
                                    '                                            <option value="0">阴性</option>' +
                                    '                                            <option value="1">阳性</option>' +
                                    '                                        </select>' +
                                    '                                    </div>' +
                                    '                                    <div class="layui-input-inline" style="height: 38px;line-height: 38px;">' +
                                    '                                        参考值：' + v.standardValues +
                                    '                                    </div>' +
                                    '                                </div>' +
                                    '                                <div >' +
                                    '                                    <div >' +
                                    '                                        <input type="text" name="description"  placeholder="详细描述或建议,无则不填" autocomplete="off" class="layui-input" maxlength="255">' +
                                    '                                    </div>' +
                                    '                                </div>' +
                                    '                            </div>'
                                );
                            });
                            $(".healthDatabox").append(' <div style="margin-top:20px;"><button type="button" class="layui-btn"   id="saveItems">确认</button><a id="cancel" class="layui-btn layui-btn-normal saveItems" href="javascript:$.pageslide.close()">取消</a></div> ');
                            $(":checkbox").attr("disabled", "disabled");
                            $("#sub").attr("disabled", "disabled");
                            $("#cz").attr("disabled", "disabled");

                            //取消填写信息体检详细信息
                            $("#cancel").on("click", function () {
                                $(data.elem).prop("checked", false);
                                $(":checkbox").removeAttr("disabled", "disabled");
                                $("#sub").removeAttr("disabled", "disabled");
                                $("#cz").removeAttr("disabled", "disabled");
                                form.render('checkbox');
                            });

                            $('#saveItems').on("click", function () {
                                var serializeArray = $('#pageslide .healthDatabox').serializeArray();
                                var json1 = {};
                                var json2 = {};
                                var json3 = {};
                                $.each(serializeArray, function (i) {
                                    if (i <= 6) {
                                        json1[this.name] = this.value;
                                    }
                                    if (i > 6 && i<=13) {
                                        json2[this.name] = this.value;
                                    }
                                    if (i > 13 ) {
                                        json3[this.name] = this.value;
                                    }
                                });
                                if (json1.value ==""||json1.value==undefined){
                                    layer.msg("请填写必填项,*号代表必填")
                                    return;
                                }
                                if (JSON.stringify(json2) != ("{}")) {
                                    if (json2.value ==""||json2.value==undefined) {
                                        layer.msg("请填写必填项,*号代表必填")
                                        return;
                                    }
                                    healthDataArray.push(JSON.stringify(json2));
                                }
                                if (JSON.stringify(json3) != ("{}")) {
                                    if (json3.value ==""||json3.value==undefined) {
                                        layer.msg("请填写必填项,*号代表必填")
                                        return;
                                    }
                                    healthDataArray.push(JSON.stringify(json3));
                                }
                                healthDataArray.push(JSON.stringify(json1));

                                $(".healthDatabox").empty();
                                $(":checkbox").removeAttr("disabled", "disabled");
                                $("#sub").removeAttr("disabled", "disabled");
                                $("#cz").removeAttr("disabled", "disabled");
                                $.pageslide.close();
                            });
                            $("#showsild").click();
                        }
                    }
                });
            }
        }
    });

})
;